package com.xtc.netty.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class ProtobufTest {

    private static byte[] encode(SubscribeReqProto.SubscribeReq req) {
        return req.toByteArray();
    }

    private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubscribeReqProto.SubscribeReq.parseFrom(body);
    }

    private static SubscribeReqProto.SubscribeReq createSubscribeReq() {
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
        builder.setSubReqId(1)
                .setUserName("nickName")
                .setProductName("netty Book");

        List<String> addressList = new ArrayList<>();
        addressList.add("NanJing");
        addressList.add("BeiJing");
        builder.addAllAddress(addressList);
        return builder.build();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubscribeReqProto.SubscribeReq reqOne = createSubscribeReq();
        System.out.println(reqOne.toString());

        SubscribeReqProto.SubscribeReq reqTwo = decode(encode(reqOne));
        System.out.println(reqTwo.toString());

        System.out.println("Assert equal: " + reqTwo.equals(reqOne));
    }
}
